﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using POSSystem.BLL;
using MyGeneration.dOOdads;

namespace WebUI.EditPages
{
    public partial class UpdateStatus :BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);
        }

        protected void btnOKGA_OnClick(object sender, EventArgs e)
        {
            TransactionMgr tx = TransactionMgr.ThreadTransactionMgr();
            try
            {
                if (Request.QueryString[ID] == null || Request.QueryString[ID] == "")
                {
                    CloseDlg(0);
                    return;
                }

                int id = int.Parse(Request.QueryString[ID]);
                RepairJob job = new RepairJob();
                job.LoadByPrimaryKey(id);

                tx.BeginTransaction();
                if (!string.IsNullOrEmpty(job.s_StatusID))
                    job.PreviousStatusID = job.StatusID;
                job.StatusID = RepairStatus.OKGA;
                job.LastModifiedByUser = SessionUser.ID;
                job.LastModifiedDate = DateTime.UtcNow.AddHours(SysVars.TimeZone);
                if (txtNote.Text.Trim() != "")
                    job.Note = RepairJob.DrawMemo(txtNote.Text.Trim(), SessionUser.FullName) + job.s_Note;

                job.Save();

                RepairStatusHistories hist = new RepairStatusHistories();
                hist.AddNew();
                hist.BranchID = Branches.CurrentBranchID;
                hist.RepairJobID = job.ID;
                hist.RepairStatusID = job.StatusID;
                hist.UpdatedByUser = SessionUser.ID;
                hist.UpdatedDate = DateTime.UtcNow.AddHours(SysVars.TimeZone);
                hist.Save();
    
                tx.CommitTransaction();
                CloseDlg(1);
            }
            catch (Exception ex)
            {
                tx.RollbackTransaction();
                ErrorLog.WriteToFile("UpdateStatus.aspx.cs", "btnSaveAndClose_OnClick", ex.StackTrace);
                ((Popup)Master).ShowErrorMessage("Error: " + ex.Message);
            }
        }

        protected void btnSaveAndClose_OnClick(object sender, EventArgs e)
        {
            TransactionMgr tx = TransactionMgr.ThreadTransactionMgr();
            try
            {
                if (Request.QueryString[ID] == null || Request.QueryString[ID] == "")
                {
                    CloseDlg(0);
                    return;
                }

                int id = int.Parse(Request.QueryString[ID]);
                RepairJob job = new RepairJob();
                job.LoadByPrimaryKey(id);

                tx.BeginTransaction();

                if (hidTestOnly.Value == "0" && hidStatusID.Value != "0" && hidStatusID.Value != "")
                {
                    if (!string.IsNullOrEmpty(job.s_StatusID))
                        job.PreviousStatusID = job.StatusID;
                    job.s_StatusID = hidStatusID.Value;
                    job.LastModifiedByUser = SessionUser.ID;
                    job.LastModifiedDate = DateTime.UtcNow.AddHours(SysVars.TimeZone);
                    if (txtNote.Text.Trim() != "")
                        job.Note = RepairJob.DrawMemo(txtNote.Text.Trim(), SessionUser.FullName) + job.s_Note;

                    job.Save();

                    if (job.StatusID == RepairStatus.Rejected)
                    {
                        RepairJobProblems.SetRejectedPrice(job.ID);
                    }
                    else if (job.PreviousStatusID == RepairStatus.Rejected)
                        RepairJobProblems.RestorePrice(job.ID);

                    RepairStatusHistories hist = new RepairStatusHistories();
                    hist.AddNew();
                    hist.BranchID = Branches.CurrentBranchID;
                    hist.RepairJobID = job.ID;
                    hist.s_RepairStatusID = hidStatusID.Value;
                    hist.UpdatedByUser = SessionUser.ID;
                    hist.UpdatedDate = DateTime.UtcNow.AddHours(SysVars.TimeZone);
                    hist.Save();
                }
                else if (txtNote.Text.Trim() != "")
                {
                    job.Note = RepairJob.DrawMemo(txtNote.Text.Trim(), SessionUser.FullName) + job.s_Note;
                    job.LastModifiedByUser = SessionUser.ID;
                    job.LastModifiedDate = DateTime.UtcNow.AddHours(SysVars.TimeZone);
                    job.Save();
                }

                tx.CommitTransaction();
                CloseDlg(1);
            }
            catch (Exception ex)
            {
                tx.RollbackTransaction();
                ErrorLog.WriteToFile("UpdateStatus.aspx.cs", "btnSaveAndClose_OnClick", ex.StackTrace);
                ((Popup)Master).ShowErrorMessage("Error: " + ex.Message);
            }
        }
    }
}